package test;

import log.LogScreen;
import log.Logger;
import net.rim.device.api.i18n.MissingResourceException;
import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.TransitionContext;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import runnables.ActualizarPushRunnable;
import runnables.DesSuscribirPushRunnable;
import runnables.SuscribirPushRunnable;
import screens.ConfigurationScreen;
import screens.NotificationScreen;
import screens.PleaseWaitPopupScreen;
import strings.WebcampusResource;
import webservice.WebServiceURLs;

import com.rim.samples.device.push.PushController;

public class Menues implements WebcampusResource{

	// RESOURCES
	private static ResourceBundle resources = ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);

	// Menu Item for 'Configuracion'
	static final class ConfigMenuItem extends MenuItem
	{
		public ConfigMenuItem(String text, int ordinal, int priority){
			super(text, ordinal, priority);
		}
		public void run() {
			UiApplication.getUiApplication().pushScreen(new ConfigurationScreen());
		}
	} 

	public static ConfigMenuItem getConfigMenuItem(String text,int ordinal, int priority){
		return new ConfigMenuItem(text, ordinal, priority);
	}

	// Menu Item for 'About'
	static final class AboutMenuItem extends MenuItem
	{
		AboutMenuItem(String text, int ordinal, int priority){
			super(text, ordinal, priority);
		}
		public void run() {
			Dialog.inform(resources.getString(ABOUT_INFO));
		}
	} 

	public static AboutMenuItem getAboutMenuItem(String text,int ordinal, int priority){
		return new AboutMenuItem(text, ordinal, priority);
	}

	// Menu Item for 'Logout'
	static final class LogoutMenuItem extends MenuItem
	{
		LogoutMenuItem(String text, int ordinal, int priority){
			super(text, ordinal, priority);
		}
		public void run() {
			new ActualizarPushRunnable(WebServiceURLs.PUSH_STATUS_PAUSED).run();
			Logger.log("paused status done");
			Screen screen = UiApplication.getUiApplication().getActiveScreen();
			while (screen!=null){
				UiApplication.getUiApplication().popScreen(screen);
				screen = UiApplication.getUiApplication().getActiveScreen();
			}
			UiApplication.getUiApplication().pushScreen(new ConfigurationScreen());
			Logger.log("ui logout is done");
		}
	}

	public static LogoutMenuItem getLogoutMenuItem(String text,int ordinal, int priority){
		return new LogoutMenuItem(text, ordinal, priority);
	}

	// Menu Item for 'Activate push'
	static final class ActivatePushMenuItem extends MenuItem
	{
		public ActivatePushMenuItem(String text, int i, int j){
			super(text, i, j);
		}
		public void run() {
			SuscribirPushRunnable apr = new SuscribirPushRunnable(false);
			Logger.log("starting suscribir push");
			try {
				PleaseWaitPopupScreen.showScreenAndWait(apr, resources.getString(POPUP_PUSH_SUBSCRIBING));
				PushController.register();
			} catch (MissingResourceException e){
				Logger.log("suscribir push missing resource exception");
				PleaseWaitPopupScreen.showScreenAndWait(apr, "Suscribiendo...");
				PushController.register();
			}
		}
	} 

	public static ActivatePushMenuItem getActivatePushMenuItem(String text, int i, int j){
		return new ActivatePushMenuItem(text,i,j);
	}

	// Menu Item for 'Deactivate push'
	static final class DeActivatePushMenuItem extends MenuItem
	{
		public DeActivatePushMenuItem(String text, int i, int j){
			super(text, i, j);
		}
		public void run() {
			DesSuscribirPushRunnable dpr = new DesSuscribirPushRunnable();
			Logger.log("starting dessuscribir push");
			//TEST
//			PushController.unregister();
			try {
				PleaseWaitPopupScreen.showScreenAndWait(dpr, resources.getString(POPUP_PUSH_UNSUBSCRIBING));
				PushController.unregister();
			} catch (MissingResourceException e){
				Logger.log("dessuscribir push missing resource exception");
				PleaseWaitPopupScreen.showScreenAndWait(dpr, "Desuscribiendo...");
				PushController.unregister();
			}
		}
	} 

	public static DeActivatePushMenuItem getDeActivatePushMenuItem(String text, int i, int j){
		return new DeActivatePushMenuItem(text,i,j);
	}

	// Menu Item for Log screen
	static final class LogMenuItem extends MenuItem
	{
		public LogMenuItem(String text, int ordinal, int priority){
			super(text, ordinal, priority);
		}
		public void run() {
			UiApplication.getUiApplication().pushScreen(new LogScreen());
		}
	} 

	public static LogMenuItem getLogMenuItem(String text,int ordinal, int priority){
		return new LogMenuItem(text, ordinal, priority);
	}
	
	// Menu Item for Push screen
	static final class NotificationsMenuItem extends MenuItem
	{
		public NotificationsMenuItem(String text, int ordinal, int priority){
			super(text, ordinal, priority);
		}
		public void run() {
			UiApplication.getUiApplication().pushScreen(new NotificationScreen(TransitionContext.TRANSITION_FADE));
		}
	} 
	public static NotificationsMenuItem getNotificationsMenuItem(String text,int ordinal, int priority){
		return new NotificationsMenuItem(text, ordinal, priority);
	}

}
